package at.fhooe.pandora.states;

import at.fhooe.engine.event.events.KeyDownEvent;
import at.fhooe.engine.event.events.WindowCloseRequestEvent;
import at.fhooe.engine.pandora.Game;
import at.fhooe.engine.state.State;
import at.fhooe.engine.state.StateDataObject;
import at.fhooe.pandora.views.MenuView;

public class MenuState implements State, KeyDownEvent.Listener {
	
	public static final String NAME = "MenuState";

	private MenuView m_menuView;
	
	public MenuState() {
		m_menuView = new MenuView();
	}
	
	public String getName() {
		return NAME;
	}
	
	public static final String MENU_TIMER = "Menu Timer";
	public void engage() {
		addListeners();
		
		m_menuView.engage();
	}
	
	public void engage(StateDataObject data) {
		engage();
	}
	
	public void disengage() {
		removeListeners();
		
		Game.getInstance().getEventManager().clearEventsForDispatch();
		Game.getInstance().getEntityManager().clearEntitiesToEngage();
		Game.getInstance().getEntityManager().removeAllEntities();
		
		m_menuView.disengage();
	}
	
	private void addListeners() {
		Game.getInstance().getEventManager().addListener(this, KeyDownEvent.TYPE);
	}
	
	private void removeListeners() {
		Game.getInstance().getEventManager().removeListener(this, KeyDownEvent.TYPE);
	}
	
	public void onKeyDownEvent(KeyDownEvent keyDownEvent) {
		if(keyDownEvent.getKeyCode() == 27) {			// Esc
			Game.getInstance().getEventManager().dispatchEvent(new WindowCloseRequestEvent());
		} else if(keyDownEvent.getKeyCode() == 10) {	// Enter
			Game.getInstance().getStateManager().switchToState(PlayState.NAME);
		}
	}
	
}
